class Shape{
	var numberOfSides = 0 
	func simpleDescription() -> String{
		return "A Shape with \(numberOfSides) sides."
	}
}

var shape = Shape()

shape.numberOfSides = 3

var desc = shape.simpleDescription()

print(desc)

class NamedShape{
	var numberOfSides : Int = 0 
	var name : String = ""
	
	init(){
		numberOfSides = 0 
	}
	
	init(name:String){
		self.name = name
	}
	
	func simpleDescription() -> String{
		return "A Shape named \(self.name) with \(numberOfSides) sides."
	}
}

var nameShape = NamedShape()

print("\(nameShape.simpleDescription())")

nameShape = NamedShape(name:"Triangle")
nameShape.numberOfSides = 3

print("\(nameShape.simpleDescription())")

//类的继承
class Square : NamedShape{
	var sideLength:Double = 0.0
	 
	init(sideLength:Double,name:String){
		self.sideLength = sideLength
		super.init(name:name)
		numberOfSides = 4 
		
		
	}
	
	func area() -> Double{
		return sideLength * sideLength
	}
	
	override func simpleDescription() -> String{
		return "A square with sides of length \(sideLength)"

	}
}

let test = Square(sideLength:5.2,name:"My Test square")

print(test.area())
print(test.simpleDescription())



//getter setter 

class EquilateralTriangle:NamedShape{
	var sideLength:Double = 0.0
	//var author:String = ""
	init(sideLength:Double,name:String){
		self.sideLength = sideLength
		super.init(name:name)
	}
	
	//计算属性
	var perimeter:Double{
		get{
			return 3.0 * sideLength
		}
		set{
			sideLength = newValue / 3.0
		}
		
	}
	
	var authorName:String{
		get{
			return "zhupei"//默认
		}
		
		set{
			print("newValue = \(newValue)")
			//self.authorName = newValue
		}
	}
	
	override func simpleDescription() -> String{
		return "An equilateral triangle with sides of length \(sideLength)."
	}

}

var triangle = EquilateralTriangle(sideLength:3.1,name:"a triangle")

print(triangle.perimeter)//getter


triangle.perimeter = 9.9 //setter,newValue = 9.9

print(triangle.sideLength)
print("authorName : \(triangle.authorName)")
triangle.authorName = "Apache"
print("authorName : \(triangle.authorName)")



//willset 

class TriangleAndSquare{
	var triangle:EquilateralTriangle{
		willSet{
			square.sideLength = newValue.sideLength
			
		}
	}
	
	var square: Square{
		willSet{
			triangle.sideLength = newValue.sideLength
		}
	}
	
	init(size:Double,name:String){
		square = Square(sideLength:size,name:name)
		triangle = EquilateralTriangle(sideLength:size,name:name)
	}
}

var triangleAndSquare = TriangleAndSquare(size:10,name:"another test shape")

print(triangleAndSquare.square.sideLength)
print(triangleAndSquare.triangle.sideLength)


triangleAndSquare.square = Square(sideLength: 50, name: "larger square")

print(triangleAndSquare.square.sideLength)
print(triangleAndSquare.triangle.sideLength)

let optionalSquare:Square? = Square(sideLength:3.5,name:"optional square")

let sideLength = optionalSquare?.sideLength

print("optional sideLength \(sideLength)")



class People
{
	//普通属性
	var firstName:String = ""
	var lastName:String  = ""
	var nickName:String  = ""
	
	//计算属性
	var fullName:String
	{
		get
		{
			return nickName + " " + firstName + " " + lastName
		}
	}
	
	//带属性监视器的普通属性
	var age:Int = 0
	{
		//我们需要在age属性变化前做点什么
		willSet
		{
			print("Will set an new value \(newValue) to age")
		}
		//我们需要在age属性发生变化后，更新一下nickName这个属性
		didSet
		{
			print("age filed changed form \(oldValue) to \(age)")
			if age<10
			{
				nickName = "Little"
			}else
			{
				nickName = "Big"
			}
		}
	}
	
	func toString() -> String
	{
		return "Full Name: \(fullName) " + ", Age: \(age) "
	}
	
}

var me = People()
me.firstName = "Zhang"
me.lastName  = "San"
me.age = 7
print(me.toString())

/*程序输出
Will set an new value 20 to age
age filed changed form 0 to 20
Full Name: Big Zhang San , Age: 20
*/















